population_vars <-c("rhs_lnnewpop",
                     "rhs_largemetro",
                     "rhs_popdensity",
                     "rhs_lnnewpopGrL0")

violence_vars <- paste(rep(paste("rhs_",
                                   c("gueratt",
                                     "paratt",
                                     "clashes",
                                     "casualties",
                                     "govatt",
                                     "parmass",
                                     "guermass",
                                     "parattinf",
                                     "parattni",
                                     "attacks",
                                     "violence",
                                     "violenceDUM",
                                     "violenceHIGHDUM",
                                     "violspike"),
                                   sep=""),
                             each=2),
                         c("L0", "L1"),
                         sep="")

remoteness_vars <- c("rhs_p_main_roads",
                     "rhs_np_main_roads",
                     "rhs_p_sec_roads",
                     "rhs_np_sec_roads",
                     "rhs_dirt_roads",
                     "rhs_den_p_main_roads",
                     "rhs_den_np_main_roads",
                     "rhs_den_p_sec_roads",
                     "rhs_den_np_sec_roads",
                     "rhs_den_dirt_roads")
elections_vars <- c("rhs_elec_al_turnout",
                    "rhs_elec_ca_turnout",
                    "rhs_elec_se_turnout",
                    "rhs_elec_d_nat",
                    "rhs_elec_d_president",
                    "rhs_elec_d_loc",
                    "rhs_elec_d_mayor",
                    "rhs_elec_hhi_ca",
                    "rhs_elec_F_ca",
                    "rhs_elec_P_ca",
                    "rhs_elec_hhi_se",
                    "rhs_elec_F_se", 
                    "rhs_elec_P_se",
                    "rhs_elec_margin_1_ca",
                    "rhs_elec_margin_1_se",
                    "rhs_elec_s_1st_al",
                    "rhs_elec_s_1st_ca",
                    "rhs_elec_s_2nd_ca",
                    "rhs_elec_s_1st_se",
                    "rhs_elec_s_2nd_se",
                    "rhs_elec_leaning_1_ca_Rest", 
                    "rhs_elec_leaning_1_ca_CRight",
                    "rhs_elec_leaning_1_ca_CLeft", 
                    "rhs_elec_leaning_1_se_Rest",
                    "rhs_elec_leaning_1_se_CRight", 
                    "rhs_elec_leaning_1_se_CLeft",
                    "rhs_elec_leaning_2_ca_Rest", 
                    "rhs_elec_leaning_2_ca_CRight",
                    "rhs_elec_leaning_2_ca_CLeft",
                    "rhs_elec_leaning_2_se_Rest",
                    "rhs_elec_leaning_2_se_CRight", 
                    "rhs_elec_leaning_2_se_CLeft",
                    "rhs_elec_party_1st_al_Other", 
                    "rhs_elec_party_1st_al_Liberal",
                    "rhs_elec_party_1st_al_Conserv", 
                    "rhs_elec_party_1st_ca_Other",
                    "rhs_elec_party_1st_ca_Liberal",
                    "rhs_elec_party_1st_ca_Conserv",
                    "rhs_elec_party_1st_se_Other", 
                    "rhs_elec_party_1st_se_Liberal",
                    "rhs_elec_party_1st_se_Conserv", 
                    "rhs_elec_party_2nd_ca_Other",
                    "rhs_elec_party_2nd_ca_Liberal", 
                    "rhs_elec_party_2nd_ca_Conserv",
                    "rhs_elec_party_2nd_se_Other", 
                    "rhs_elec_party_2nd_se_Liberal",
                    "rhs_elec_party_2nd_se_Conserv")

geography_vars <- c("rhs_areakm",
                    "rhs_inhabitable",
                    "rhs_optimal_sugar",
                    "rhs_suboptimal_sugar",
                    "rhs_suitable_sugar",
                    "rhs_nosuitable_sugar",
                    "rhs_optimal_palm",
                    "rhs_suboptimal_palm",
                    "rhs_suitable_palm",
                    "rhs_nosuitable_palm",
                    "rhs_averageslope",
                    "rhs_stdslope",
                    "rhs_slightlyflatp",
                    "rhs_slightlyslopedp",
                    "rhs_moderatelyslopedp",
                    "rhs_stronglyslopedp",
                    "rhs_slightlysteepp",
                    "rhs_moderatelysteepp",
                    "rhs_stronglysteepp",
                    "rhs_averagewater",
                    "rhs_stdwater",
                    "rhs_mriv",
                    "rhs_sriv",
                    "rhs_triv",
                    "rhs_dmriv2",
                    "rhs_dsriv2",
                    "rhs_dtriv2",
                    "rhs_splain",
                    "rhs_shill",
                    "rhs_smount",
                    "rhs_svall",
                    "rhs_swat",
                    "rhs_shill2",
                    "rhs_smount2",
                    "rhs_srug",
                    "rhs_maxslop")

governmentfinance_vars <- c("rhs_dnprevtot",
                            "rhs_dnpspendtot",
                            "rhs_dnprevcurrent",
                            "rhs_dnpspendcurrent",
                            "rhs_dnpdeficitcurr",
                            "rhs_dnpspendcap",
                            "rhs_dnprevtax",
                            "rhs_dnprevnontax",
                            "rhs_dnpspendfun",
                            "rhs_dnpspendcapoth",
                            "rhs_dnprevtaxland",
                            "rhs_dnprevtaxind",
                            "rhs_dnprevtaxoth",
                            "rhs_dnpspendperson",
                            "rhs_dnprevcapL0",
                            "rhs_dnprevcapL1",
                            "rhs_dnprevcapL2",
                            "rhs_dnpdeficittotL0",
                            "rhs_dnpdeficittotL1",
                            "rhs_dnpdeficittotL2",
                            "rhs_dnpfinancingL0",
                            "rhs_dnpfinancingL1",
                            "rhs_dnpfinancingL2",
                            "rhs_dnprevcurtranL0",
                            "rhs_dnprevcurtranL1",
                            "rhs_dnprevcurtranL2",
                            "rhs_dnpspendcapformL0",
                            "rhs_dnpspendcapformL1",
                            "rhs_dnpspendcapformL2",
                            "rhs_dnpcreditL0",
                            "rhs_dnpcreditL1",
                            "rhs_dnpcreditL2",
                            "rhs_dnpbalL0",
                            "rhs_dnpbalL1",
                            "rhs_dnpbalL2",
                            "rhs_dnpspendgenL0",
                            "rhs_dnpspendgenL1",
                            "rhs_dnpspendgenL2",
                            "rhs_dnpspendtranL0",
                            "rhs_dnpspendtranL1",
                            "rhs_dnpspendtranL2",
                            "rhs_nationexp")
history_vars <- c("rhs_CrownEmployees",
                 "rhs_NonMilEmployees",
                 "rhs_ColStatePresence",
                 "rhs_CityStatus",
                 "rhs_RoyalRoad",
                 "rhs_SlaveShare1843",
                 "rhs_SlavesDum1843",
                 "rhs_Indians",
                 "rhs_Encomiendas",
                 "rhs_EncomiendasDum",
                 "rhs_GoldMineDum",
                 "rhs_FoundDate",
                 "rhs_Pop1843",
                 "rhs_MissPop1843")


distribution_vars <- c("rhs_gini_land",
                       "rhs_ubn93",
                       "rhs_lqi93")
comshock_interaction_vars <- paste(rep(paste("rhs_",
                                            c("cofintxlinternalp",
                                              "oilprod88xlop",
                                              "mining78xlgoldp",
                                              "coalres78xlcoalp",
                                              "mining78xlsilverp",
                                              "mining78xlplatp"),
                                            sep=""),
                                      each=3),
                                  c("L0", "L1", "L2"),
                                  sep="")
commprice_vars <- paste(rep(paste("rhs_",
                                  c("linternalp",
                                    "lop",
                                    "lgoldp",
                                    "lcoalp",
                                    "lsilverp",
                                    "lplatp"),
                                  sep=""),
                            each=3),
                        c("L0", "L1", "L2"),
                        sep="")

comshock_vars <- c(comshock_interaction_vars,
                   commprice_vars)
production_vars <- c("rhs_oilprod88",
                    "rhs_coalres78",
                    "rhs_mining78",
                    "rhs_gold_dept87",
                    "rhs_coal_dept90",
                    "rhs_cofint")

comNoInt_vars <- c(commprice_vars,
                   production_vars)

comshockAll_vars <- c(comshock_vars, production_vars)

illicitproduction_vars <- c("rhs_hpoppy1994",
                           "rhs_dpoppy1994",
                           "rhs_hcoca1994",
                           "rhs_dcoca1994",
                           "rhs_errad1994",
                           "rhs_derrad1994")
illicitprice_vars <- paste(rep(paste("rhs_",
                                    c("cocahec_col",
                                      "coke_col",
                                      "coca_eradcol",
                                      "coca_leafcol",
                                      "cocaine_prodcol",
                                      "lcpre",
                                      "lcpwu",
                                      "lcpwe",
                                      "cocahec_bol",
                                      "cocahec_peru",
                                      "cocahec_tot",
                                      "cocaleaf_tot",
                                      "coke_bol",
                                      "coke_peru",
                                      "coke_tot",
                                      "coke_notcol",
                                      "coca_eradbol",
                                      "coca_leafbol",
                                      "cocaine_prodbol",
                                      "cocaine_szdcol",
                                      "coca_eradperu",
                                      "coca_leafperu",
                                      "cocaine_prodperu",
                                      "cocaine_szdperu"),
                                    sep=""),
                              each=3),
                          c("L0", "L1", "L2"),
                          sep="")
illicit_interaction_vars <- c("rhs_cocaxwcocapusL0",
                              "rhs_cocaxwcocapusL1",
                              "rhs_cocaxwcocapusL2")

illicitshock_vars <- c(illicitprice_vars, illicit_interaction_vars)

illicit_all_vars <- c(illicitshock_vars, illicitproduction_vars)
rhs.illicitNoInt <- c(illicitprice_vars, illicitproduction_vars)


climateshock_vars <- c("rhs_rain_deviationL0",
                       "rhs_rain_deviationL1",
                       "rhs_rain_deviationL2",
                       "rhs_temp_deviationL0",
                       "rhs_temp_deviationL1",
                       "rhs_temp_deviationL2")

climatehist_vars <- c("rhs_rain_average",
                        "rhs_rain_sd",
                        "rhs_temp_average",
                        "rhs_temp_sd")

dmz_vars <- c("rhs_dmz_dum",
             "rhs_dmz_distance")
baseweights_vars <- c("rhs_usmil_bases6")
basetime_vars <- paste(rep(paste("rhs_",
                                 c("usmil_lrmil_col",
                                   "usmil_lrnar_col",
                                   "usmil_lrmilnar_col"),
                                 sep=""),
                           each=3),
                       c("L0", "L1", "L2"),
                       sep="")
base_interaction_vars <- paste(rep(paste("rhs_usmil_bases6x",
                                         c("lrmilnar_col",
                                           "lrmil_col",
                                           "lrnar_col"),
                                         sep=""),
                                   each=3),
                               c("L0", "L1", "L2"),
                               sep="")
bases_vars <- c(basetime_vars, baseweights_vars, base_interaction_vars)
bases_noint_vars <- c(basetime_vars, baseweights_vars)
bases_noweight_vars <- c(basetime_vars, base_interaction_vars)


# Make larger groups of variables

varying_vars <- c(comshock_vars,
                  governmentfinance_vars,
                  illicitshock_vars,
                  climateshock_vars,
                  bases_vars,
                  elections_vars)
fixed_vars <- c(population_vars,
                production_vars,
                geography_vars,
                distribution_vars,
                illicitproduction_vars,
                remoteness_vars,
                climatehist_vars,
                history_vars,
                dmz_vars)
full_vars <- c(violence_vars,
               varying_vars,
               fixed_vars)

table1 <- list(full = full_vars)
table3_pt1 <- list(onlyviolence = violence_vars,
                   violpop = c(population_vars,
                               violence_vars))
table3_pt2 <- list(noviolence = c(varying_vars,
                                  fixed_vars))
table3_pt3 <- list(fullvarying = varying_vars)
table3_pt4 <- list(fullfixed = fixed_vars)

table3 <- c(table3_pt1,
            table3_pt2,
            table3_pt3,
            table3_pt4)
table4 <- list(full = full_vars,
               onlyviolence = violence_vars,
               violpop = c(population_vars,
                           violence_vars),
               noviolence = c(varying_vars,
                              fixed_vars))
table4_pt1 <- list(full = full_vars,
                   onlyviolence = violence_vars)
table4_pt2 <- list(violpop = c(population_vars,
                               violence_vars),
                   noviolence = c(varying_vars,
                                  fixed_vars))

figures_1_2_annual <- list(comshock=c(population_vars,
                                      comshock_vars),  
                           climateshock=c(population_vars,
                                          climateshock_vars),
                           elections=c(population_vars, elections_vars),
                           governmentfinance=c(population_vars, governmentfinance_vars),
                           bases=c(population_vars, bases_vars),
                           illicitshock=c(population_vars, illicitshock_vars))
figures_1_2_slow <- list()

figures_1_2_fixed <- list(population=population_vars,
                          production=c(population_vars, production_vars),
                          distribution=c(population_vars, distribution_vars),
                          climatehist=c(population_vars, climatehist_vars),
                          history=c(population_vars, history_vars),
                          dmz=c(population_vars, dmz_vars),
                          remoteness=c(population_vars, remoteness_vars),
                          geography=c(population_vars, geography_vars),
                          illicitproduction=c(population_vars, illicitproduction_vars)
                          )
figure_A5 <- list(comprice = c(population_vars,
                               commprice_vars),
                  com_all = c(population_vars,
                              comshockAll_vars),
                  base_weight = c(population_vars,
                                  baseweights_vars),
                  mil_spend = c(population_vars,
                                basetime_vars),
                  bases_noweight = c(population_vars,
                                     bases_noweight_vars),
                  illicitprice = c(population_vars,
                                   illicitprice_vars),
                  illicit_all = c(population_vars,
                                  illicit_all_vars))

shock_breakdown <- c('production', 
                     'comprice',
                     'comshock',
                     'com_all',
                     'base_weight',
                     'mil_spend',
                     'bases_noweight',
                     'bases',
                     'illicitproduction',
                     'illicitprice',
                     'illicitshock',
                     'illicit_all')

figures_1_2 <- c(figures_1_2_fixed,
                 figures_1_2_slow,
                 figures_1_2_annual)

figure_1 = figures_1_2
figure_1_fixed = figures_1_2_fixed
figure_1_slow = figures_1_2_slow
figure_1_annual = figures_1_2_annual

figure_2 = figures_1_2

figure_5A <- list(comprice = c(population_vars,
                               commprice_vars),
                  com_all = c(population_vars,
                              comshockAll_vars),
                  base_weight = c(population_vars,
                                  baseweights_vars),
                  mil_spend = c(population_vars,
                                basetime_vars),
                  bases_noweight = c(population_vars,
                                     bases_noweight_vars),
                  illicitprice = c(population_vars,
                                   illicitprice_vars),
                  illicit_all = c(population_vars,
                                  illicit_all_vars))

tableA2 = table1

tableA3_pt1 = table1
tableA3_pt2 = table3_pt1
tableA3_pt3 = table3_pt2
tableA3_pt4 = table3_pt3
tableA3_pt5 = table3_pt4

figure_A1_fixed = figures_1_2_fixed
figure_A1_slow = figures_1_2_slow
figure_A1_annual = figures_1_2_annual

tableA7 = table1
figure_A3_fixed = figures_1_2_fixed
figure_A3_slow = figures_1_2_slow
figure_A3_annual = figures_1_2_annual

labels = c(comshock = "Commodity Shocks",
           production = "Commodity Production",
           governmentfinance = "Government Finance",
           education = "Education",
           geography = "Geography",
           distribution = "Distributional Measures",
           remoteness = "Remoteness",
           illicitproduction = "Drug Production",
           illicitshock = "Drug Production Shocks",
           climateshock = "Weather Shocks",
           climatehist = "Weather History",
           history = "Historical Traits",
           dmz = "DMZ Proximity",
           bases = "US Military Involvement",
           elections = "Electoral",
           priceOnly = "Comm Prices",
           comshockAll = "Comm Prices, Wts, and Ints",
           priceavg ="Commodities with MAs",
           weatheravg = "Weather with MAs")

labels_A5 = c(comshock = "Comm. Prices and Ints",
              production = "Commodity Production (Wts)",
              governmentfinance = "Government Finance",
              education = "Education",
              geography = "Geography",
              distribution = "Distributional Measures",
              remoteness = "Remoteness",
              illicitproduction = "Drug Production Wts",
              illicitprice="Drug Prices",
              illicitshock = "Drug Prices and Ints",
              illicit_all = "Drug Prices, Wts and Ints",
              climateshock = "Weather Shocks",
              climatehist = "Weather History",
              history = "Historical Traits",
              dmz = "DMZ Proximity",
              base_weight = "Base Presence",
              bases = "US Mil. Spend, Wts, and Ints",
              bases_noweight = "US Mil. Spend and Ints",
              mil_spend = "US Mil. Spending (No Int)",
              elections = "Electoral",
              comprice = "Comm. Prices",
              com_all = "Comm Prices, Wts, and Ints")

if (length(full_vars[!(full_vars %in% colnames(dta))]) > 0) {
  print('Missing:')
  print(full_vars[!(full_vars %in% colnames(dta))])
}


summary_categories <- list(comshock=comshock_vars,
                           govfin=governmentfinance_vars,
                           illshock=illicitshock_vars,
                           climshock=climateshock_vars,
                           bases=bases_vars,
                           elections=elections_vars,
                           pop=population_vars,
                           production=production_vars,
                           geo=geography_vars,
                           distribution=distribution_vars,
                           illprod=illicitproduction_vars,
                           remote=remoteness_vars,
                           climhist=climatehist_vars,
                           history=history_vars,
                           dmz=dmz_vars,
                           violence=violence_vars)